#!/usr/bin/python
# -*- coding: utf-8 -*-

import MySQLdb
import sys, os, datetime, dateutil
from dateutil.parser import parser

FRAME_I, FRAME_F = 33, 3863
EVENTS = 3500
CAMERA = 4

try:
    conn = MySQLdb.connect ('localhost', 'dvallejo', 'escipion', 'oculus')
except MySQLdb.Error, e:
    print "Error %d: %s" % (e.args[0], e.args[1])
    sys.exit(1)
            
cursor = conn.cursor ()

cursor.execute("""DELETE FROM Times_Summary""")

## PA processing times.
cursor.execute ("""SELECT SUM(pa_processing_time) FROM Times WHERE Camera=%s""", (CAMERA,))
row = cursor.fetchone()
pa_pt = float(row[0]) / 1000.0
print 'PA processing time: ' + str(pa_pt) + ' seconds.'

## TA processing times.
cursor.execute ("""SELECT SUM(ta_processing_time) FROM Times WHERE Camera=%s""", (CAMERA,))
row = cursor.fetchone()
ta_pt = float(row[0]) / 1000.0
print 'TA processing time: ' + str(ta_pt) + ' seconds.'

## SA processing times.
cursor.execute ("""SELECT SUM(sa_processing_time) FROM Times WHERE Camera=%s""", (CAMERA,))
row = cursor.fetchone()
sa_pt = float(row[0]) / 1000.0
print 'SA processing time: ' + str(sa_pt) + ' seconds.'

## B processing times.
cursor.execute ("""SELECT SUM(b_processing_time) FROM Times WHERE Camera=%s""", (CAMERA,))
row = cursor.fetchone()
b_pt = float(row[0]) / 1000.0
print 'B processing time: ' + str(b_pt) + ' seconds.'

## ENA processing times.
cursor.execute ("""SELECT SUM(ena_processing_time) FROM Times WHERE Camera=%s""", (CAMERA,))
row = cursor.fetchone()
ena_pt = float(row[0]) / 1000.0
print 'ENA processing time: ' + str(ena_pt) + ' seconds.'

## Store into the DB.
cursor.execute ("""INSERT INTO Times_Summary (camera,frames,pa_processing_time) VALUES (%s, %s, %s)""", (CAMERA, EVENTS, pa_pt))
cursor.execute ("""UPDATE Times_Summary SET ta_processing_time=%s WHERE camera=%s and frames=%s""", (ta_pt, CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET sa_processing_time=%s WHERE camera=%s and frames=%s""", (sa_pt, CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET b_processing_time=%s WHERE camera=%s and frames=%s""", (b_pt, CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET ena_processing_time=%s WHERE camera=%s and frames=%s""", (ena_pt, CAMERA, EVENTS))

## PA communication times.
cursor.execute ("""SELECT pa_event, client_notification FROM Times WHERE Camera=%s""", (CAMERA,))
p = parser()
pa_comm = None
errors = 0

f = open('aux.txt', 'w')

for row in cursor.fetchall():
    try:
        if pa_comm == None:
            pa_comm = p.parse(row[0]) - p.parse(row[1])
        else:
            pa_comm += (p.parse(row[0]) - p.parse(row[1]))
        f.write(str(pa_comm) + '\n')
    except ValueError:
        errors += 1

f.close()

print 'PA communication time: ' + str(pa_comm) + '.'

## TA communication times.
cursor.execute ("""SELECT ta_event, pa_notification FROM Times WHERE Camera=%s""", (CAMERA,))
ta_comm = None

for row in cursor.fetchall():
    if ta_comm == None:
        ta_comm = p.parse(row[0]) - p.parse(row[1])
    else:
        ta_comm += (p.parse(row[0]) - p.parse(row[1]))

print 'TA communication time: ' + str(ta_comm) + '.'

## SA communication times.
cursor.execute ("""SELECT sa_event, pa_notification FROM Times WHERE Camera=%s""", (CAMERA,))
sa_comm = None

for row in cursor.fetchall():
    if sa_comm == None:
        sa_comm = p.parse(row[0]) - p.parse(row[1])
    else:
        sa_comm += (p.parse(row[0]) - p.parse(row[1]))

print 'SA communication time: ' + str(sa_comm) + '.'

## B_TA communication times.
cursor.execute ("""SELECT ta_writing, b_ta_event FROM Times WHERE Camera=%s""", (CAMERA,))
b_ta_comm = None

for row in cursor.fetchall():
    if b_ta_comm == None:
        b_ta_comm = p.parse(row[0]) - p.parse(row[1])
    else:
        b_ta_comm += (p.parse(row[0]) - p.parse(row[1]))

print 'B_TA communication time: ' + str(b_ta_comm) + '.'

## B_SA communication times.
cursor.execute ("""SELECT sa_writing, b_sa_event FROM Times WHERE Camera=%s""", (CAMERA,))
b_sa_comm = None

for row in cursor.fetchall():
    if b_sa_comm == None:
        b_sa_comm = p.parse(row[0]) - p.parse(row[1])
    else:
        b_sa_comm += (p.parse(row[0]) - p.parse(row[1]))

print 'B_SA communication time: ' + str(b_sa_comm) + '.'

## ENA communication times.
cursor.execute ("""SELECT ena_event, ena_notification FROM Times WHERE Camera=%s""", (CAMERA,))
ena_comm = None

for row in cursor.fetchall():
    if ena_comm == None:
        ena_comm = p.parse(row[0]) - p.parse(row[1])
    else:
        ena_comm += (p.parse(row[0]) - p.parse(row[1]))

print 'ENA communication time: ' + str(ena_comm) + '.'

cursor.execute ("""UPDATE Times_Summary SET pa_comm=%s WHERE camera=%s and frames=%s""", (str(pa_comm), CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET ta_comm=%s WHERE camera=%s and frames=%s""", (str(ta_comm), CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET sa_comm=%s WHERE camera=%s and frames=%s""", (str(sa_comm), CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET b_ta_comm=%s WHERE camera=%s and frames=%s""", (str(b_ta_comm), CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET b_sa_comm=%s WHERE camera=%s and frames=%s""", (str(b_sa_comm), CAMERA, EVENTS))
cursor.execute ("""UPDATE Times_Summary SET ena_comm=%s WHERE camera=%s and frames=%s""", (str(ena_comm), CAMERA, EVENTS))

## TOTAL TIME SPENT.
cursor.execute ("""SELECT client_notification FROM Times WHERE Camera=%s and frame=%s""", (CAMERA, FRAME_I))
row = cursor.fetchone()
time_i = p.parse(row[0])
cursor.execute ("""SELECT ena_event FROM Times WHERE Camera=%s and frame=%s""", (CAMERA, FRAME_F))
row = cursor.fetchone()
time_f = p.parse(row[0])
total_time = time_f - time_i
cursor.execute ("""UPDATE Times_Summary SET time_i=%s, time_f=%s, time_t=%s WHERE camera=%s and frames=%s""", (str(time_i), str(time_f), str(total_time), CAMERA, EVENTS))

conn.commit()
conn.close()
